package com.heima.wemedia;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Stream;

@SpringBootTest
public class JsopDemo {

    @Test
    public void jsop() throws IOException {
        System.setProperty("webdriver.chrome.driver", "F:\\Gooledvier\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://3g.163.com/touch/ent/sub/star/?ver=c&clickfrom=index2018_header_main");
        Document document = Jsoup.parse(driver.getPageSource());
//        Document document = Jsoup.connect("https://3g.163.com/touch/ent/sub/star/?ver=c&clickfrom=index2018_header_main")
//                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36")
//                .get();

        Elements article = document.getElementsByTag("article");
        for (Element element : article) {
            Elements title1 = element.getElementsByClass("title");
            if(title1.size() >=1){
                Element element3 = title1.get(0);
                String text = element3.text();
                System.out.println("文章标题: "+text);
                // 获取到 封面
                Element element1 = element.getElementsByClass("news-pic").get(0);
                //在封面中获取到img 图片信息  有可能有多个 需要循环
                Elements imgList = element1.getElementsByTag("img");
                for (Element element2 : imgList) {
                    String attr = element2.attr("data-src");
                    String[] split = attr.split("/");
                    String string = Arrays.stream(split).limit(1).toString();
                    System.out.println(string);
                    System.out.println("=====================");
                    System.out.println(attr);
                }

            }


        }
    }
}
